import 'package:flutter/material.dart';
import 'package:flutter_study/provider/CountProvider.dart';
import 'package:provider/provider.dart';

///Provider
///1.例子CountProvider的使用
///
/// 2.多个Provider的使用 ，注意查看Main界面的{@link MultiProvider}的使用
class ProviderDemo extends StatefulWidget {
  const ProviderDemo({Key? key}) : super(key: key);

  @override
  _ProviderDemoState createState() => _ProviderDemoState();
}

class _ProviderDemoState extends State<ProviderDemo> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(appBar: AppBar(
      title: Text("provider状态管理"),centerTitle: true,
    ),
    body: Center(
      child: Column(
        children: [
          SizedBox(height:40),
          Text(Provider.of<CountProvider>(context).count.toString()),
          ElevatedButton(onPressed: (){
            Navigator.push(context, MaterialPageRoute(builder: (context){
              return ProviderDemo2();
            }));
          },
              child: Text("跳转"))
        ],
      ),
    ),
    floatingActionButton: FloatingActionButton(
      child: Icon(Icons.add),
      onPressed: (){
        context.read<CountProvider>().add();
      },
    ),
    );
  }
}



///第二个界面
///1.返回界面查看数据的变化
class ProviderDemo2 extends StatelessWidget {
  const ProviderDemo2({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("ProviderDemo2"),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: (){
          context.read<CountProvider>().add();
        },
        child: Icon(Icons.add),
      ),
    );
  }
}
